package mavenWebProject;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import mavenWebProject.user;
import mavenWebProject.userMapper;

/**
 * Servlet implementation class UpdateInfo
 */
@WebServlet("/UpdateUserInfo")
public class UpdateUserInfo extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateUserInfo() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession sqlsession=sqlSessionFactory.openSession();
		
		String password="1";
		try {
			password=request.getParameter("password1");
		}catch(Exception e) {
			password="1";
		}
		HttpSession session=request.getSession();
		String targetUsername=String.valueOf(session.getAttribute("username"));
		userMapper mapper=sqlsession.getMapper(userMapper.class);
		productsMapper pmapper=sqlsession.getMapper(productsMapper.class);
		if(password==null) {
			String username=request.getParameter("username");
			String gender=request.getParameter("gender");
			String hobbies="";
			String[] hobby= {};
			try{
			hobby=request.getParameterValues("hobbies");
			for(String s:hobby) {
				hobbies=hobbies+s+" ";
			}
			}catch(Exception e){hobbies="";}
			System.out.println(hobbies);
			int id=0;
			String tel=request.getParameter("tel");
			String location=request.getParameter("location");
			List<user> temp=mapper.selectByUsername(targetUsername);
			for(user s:temp) {
				id=s.getId();
			}
			List<user> user=mapper.selectByUsername(username);
			if(user.isEmpty() || username.contentEquals(targetUsername)) {
				mapper.updateUserInfo(username,hobbies,tel,location,gender,id);
				pmapper.updateCarUsername(username,targetUsername);
				pmapper.updateOrderUsername(username,targetUsername);
				//5.提交事务
				sqlsession.commit();
				response.setCharacterEncoding("UTF-8");
				if(username.contentEquals(targetUsername)) {
					request.getRequestDispatcher("messagePage.jsp?message=信息修改成功").forward(request, response);
				}else {
					request.getRequestDispatcher("messagePage.jsp?message=修改成功").forward(request, response);
				}
				
			}else {
				request.getRequestDispatcher("messagePage.jsp?message=该用户名太受欢迎啦，换一个吧~").forward(request, response);
			}
		}else {
			mapper.updatePassword(targetUsername, password);
			sqlsession.commit();
			request.getRequestDispatcher("messagePage.jsp?message=密码修改成功").forward(request, response);
		}
		//6.释放资源
		sqlsession.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
